package com.xzl.manger.controller;

import com.xzl.manger.dao.IAdminDao;
import com.xzl.manger.model.Admin;
import com.xzl.manger.model.Goods;
import com.xzl.manger.server.GoodsServer;
import com.xzl.manger.server.MyServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("mage")
public class Mcontroller {
    @Resource
    private MyServer myServer;
    @RequestMapping("list")
    public ModelAndView pagelist(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("Admin/AdminList");
        return mv;
    }
    @RequestMapping("pageadmin")
    public ModelAndView pageadmin(Admin ad,String root){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("Admin/AdminPage");
        List<Admin> list = myServer.selectAdmin(ad);
        List<Admin> rlist = myServer.selectRoot(new Admin());
        mv.addObject("list",list);
        mv.addObject("rlist",rlist);
        mv.addObject("root",root);
        return mv;
    }
    @RequestMapping("login")
    public ModelAndView login(Admin ad){
        ModelAndView mv = new ModelAndView();
        List<Admin> user = myServer.loginCheck(ad);
        if(user!=null){
            mv.addObject("user",user.get(0));
            if(user.get(0).getPower()==0){
                mv.addObject("root","root");
            }
            mv.setViewName("Admin/AdminList");
        }
        else {
            mv.setViewName("Admin/login");
        }
        return mv;
    }
    @RequestMapping("update")
    public void updateAdmin(Admin ad){
        Integer n = myServer.updateAdmin(ad);
        System.err.println("更新了"+n+"行");
    }
    @RequestMapping("delete")
    public void deleteAdmin(Integer id){
        Integer n = myServer.deleteAdmin(id);
        System.err.println("删除了"+n+"行");
    }
    @RequestMapping("insert")
    public void insertAdmin(Admin ad){
        Integer n = myServer.insertAdmin(ad);
        System.err.println("添加了"+n+"行");
    }
    @RequestMapping("toinsert")
    public ModelAndView toInsert(){
    ModelAndView mv = new ModelAndView();
    mv.setViewName("Admin/AdminInsert");
    return mv;
    }
}
